package org.dormi.learn.definition;

import java.util.Objects;

/**
 * @author : dormi330
 * @since : 2021/1/27/0027
 * 包含权重的边
 */
public class Edge implements Comparable<Edge> {
    public final Vertex src;
    public final Vertex dst;
    public final int weight;

    public Edge(Vertex src, Vertex dst, int weight) {
        this.src = src;
        this.dst = dst;
        this.weight = weight;
    }

    public Edge(Vertex src, Vertex dst) {
        this.src = src;
        this.dst = dst;
        this.weight = 0;
    }

    @Override
    public String toString() {
        return String.format("[%s -> %s,w=%s]", src.id, dst.id, weight);
    }

    @Override
    public int hashCode() {
        return src.id.hashCode() + dst.id.hashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == null) {
            return false;
        }
        if (!(other instanceof Edge)) {
            return false;
        }

        Edge e2 = (Edge) other;
        return Objects.equals(src, e2.src) && Objects.equals(dst, e2.dst);
    }

    /**
     * 较大的weight 具有高优先级
     */
    @Override
    public int compareTo(Edge o) {
        return o.weight - this.weight;
    }
}
